﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.HitachiServiceRef;

namespace HitachiLib.Data
{
    public class Project
    {
        private ProjectData data;

        private Dictionary<int, Project> children = new Dictionary<int,Project>();
        private Project parent;
        private Dictionary<int, Schedule> plan_schedule = new Dictionary<int, Schedule>();

        private bool selected;

        private static Dictionary<int, Project> s_map = new Dictionary<int, Project>();

        public static Project GetUnique(ProjectData pd)
        {
            if (s_map.ContainsKey(pd.ID))
            {
                return s_map[pd.ID];
            }
            Project r = new Project();
            r.data = pd;
            s_map.Add(pd.ID, r);
            return r;
        }
        public static Project GetUnique(int id)
        {
            if (s_map.ContainsKey(id))
            {
                return s_map[id];
            }
            return null;
        }
        public static List<Project> Link(ICollection<ProjectData> pdlist)
        {
            List<Project> all = new List<Project>();
            foreach (ProjectData pd in pdlist)
            {
                Project p = Project.GetUnique(pd);
                all.Add(p);
            }
            foreach (Project p in all)
            {
                if (p.Data.ParentID > 0)
                {
                    Project parent = Project.GetUnique(p.Data.ParentID);
                    if (parent != null)
                    {
                        parent.AddChild(p);
                    }
                }
            }
            return all;
        }

        public Dictionary<int, Project> Children
        {
            get { return children; }
        }

        public Project Parent
        {
            get { return parent; }
        }

        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public ProjectData Data
        {
            get { return data; }
        }

        public String Name
        {
            get { return Data.Name; }
        }

        public int Id
        {
            get { return Data.ID; }
        }

        public Dictionary<int, Schedule> PlanSchedule
        {
            get { return plan_schedule; }
        }

        public Schedule CommonSchedule
        {
            get
            {
                if (PlanSchedule.ContainsKey(Schedule.HalfMonth)) return PlanSchedule[Schedule.HalfMonth];
                return null;
            }
        }
        public Schedule YearlySchedule
        {
            get
            {
                if (PlanSchedule.ContainsKey(Schedule.WholeYear)) return PlanSchedule[Schedule.WholeYear];
                return null;
            }
        }

        public void AddChild(Project child)
        {
            if (children.ContainsKey(child.Data.ID))
            {
                return;
            }
            children.Add(child.Data.ID, child);
            child.parent = this;
        }
    }
}
